﻿<div class="row">
    <div class="col-12">
        <p>卡片形式头像框</p>
        <Upload Style="UploadStyle.Avatar" Accept="image/*" OnChange="@OnAvatarUpload" OnDelete="@(fileName => Task.FromResult(true))"></Upload>
    </div>
</div>
<div class="row mt-3">
    <div class="col-12">
        <p>圆形头像框</p>
        <Upload Style="UploadStyle.Avatar" Accept="image/*" ShowProgress="true" IsCircle="true" OnChange="@OnAvatarUpload" OnDelete="@(fileName => Task.FromResult(true))"></Upload>
    </div>
</div>
<div class="row mt-3">
    <div class="col-12">
        <p>设置 <code>IsSingle</code> 时，仅可以上传一张图片或者文件</p>
        <p>
            <div>组件提供了 <code>Accept</code> 属性用于设置上传文件过滤功能，本例中圆形头像框接受 GIF 和 JPEG 两种图像，设置 <code>Accept="image/gif, image/jpeg"</code></div>
        </p>
        <Upload Style="UploadStyle.Avatar" Accept="image/gif, image/jpeg" IsSingle="true" OnChange="@OnAvatarUpload" OnDelete="@(fileName => Task.FromResult(true))"></Upload>
    </div>
</div>

@code {
    private CancellationTokenSource? ReadAvatarToken { get; set; }
    private async Task OnAvatarUpload(IEnumerable<UploadFile> files)
    {
        // 示例代码，使用 base64 格式
        var file = files.FirstOrDefault();
        if (file != null && file.File != null)
        {
            var format = file.File.ContentType;
            if (CheckValidAvatarFormat(format))
            {
                ReadAvatarToken ??= new CancellationTokenSource();
                if (ReadAvatarToken.IsCancellationRequested)
                {
                    ReadAvatarToken.Dispose();
                    ReadAvatarToken = new CancellationTokenSource();
                }

                await file.RequestBase64ImageFileAsync(format, 640, 480, 20 * 1024 * 1024, ReadAvatarToken.Token);
            }
            else
            {
                file.Code = 1;
                file.Error = "文件格式不正确";
            }

            if (file.Code != 0)
            {
                await ToastService.Error("头像上传", $"{file.Error} {format}");
            }
        }
    }


    private bool CheckValidAvatarFormat(string format)
    {
        return "jpg;png;bmp;gif;jpeg".Split(';').Any(f => format.Contains(f, StringComparison.OrdinalIgnoreCase));
    }
}
